package com.scala.examples.concurrency;

import java.util.*;

public class PermutationsAll {
    public static void main(String[] args) {
        long start1 = System.currentTimeMillis();



        for (int y = 0; y < 50; ++y) {
            PermutationsAll.givePermutations2("", "abcdefg");
        }





        long end1 = System.currentTimeMillis();

        for (int y = 0; y < 50; ++y) {
            for (String p : PermutationsAll.givePermutations("abcdefg")) {
                System.out.println(p);
            }
        }



        long end2 = System.currentTimeMillis();
        System.out.println("Total time 2: " + (end1 - start1) / 50);
        System.out.println("Total time 1: " + (end2 - end1) / 50);
    }

    private static ArrayList<String> givePermutations(String str) {
        ArrayList<String> permus = new ArrayList<String>();
        for (int i = 0; i < str.length(); ++i) {
            if (str.length() > 2) {
                for (String p : givePermutations(str.substring(1))) {
                    permus.add(str.charAt(0) + p);
                }
            } else {
                permus.add(str);
            }
            str = str.substring(1) + str.charAt(0);
        }
        return permus;
    }

    private static void givePermutations2(String prefix, String suffix) {
        if (suffix.length() == 0) {
            System.out.println(prefix);
        } else {
            for (int i = 0; i < suffix.length(); i++) {
                givePermutations2(prefix + suffix.charAt(i), suffix.substring(0, i) + suffix.substring(i + 1, suffix.length()));
            }
        }
    }
}
